package cs2340.todolist.model.account_management;

import java.util.Date;

import android.content.Context;
import cs2340.todolist.model.Location;
import cs2340.todolist.model.sqlite_database.ToDoItemDataSource;

/**
 * In charge of validating, adding, and removing items from the database
 * @author BaoTam_Vo
 *
 */
public class ToDoItemManager {
	public static final String[] TYPES = {"Personal","School","Work"};
	public static final String[] RECUR_TYPES = {"never","daily","weekly","monthly","yearly"};
	public ToDoItemDataSource dataSource;
	
	/**
	 * instantiate a ToDoItemManager that holds a ToDoItemDataSource
	 * @param context
	 */
	public ToDoItemManager(Context context) {
		dataSource = new ToDoItemDataSource(context);
	}
	
	/**
	 * insert an item into the database
	 * @param title
	 * @param location
	 * @param type
	 * @param dueDate
	 * @param recurType
	 * @param description
	 * @return the id of the new item, -1 if failed
	 */
	private long createToDoItem(String title, Location location, int type, Date dueDate, int recurType, String description) {
		dataSource.open();
		long itemId = dataSource.insertItem((long)0, title, location, type, dueDate, recurType, description, false);
		dataSource.close();
		return itemId;
	}
	
	/**
	 * validate the title of the item
	 * @param title
	 * @return false if title is "", true otherwise
	 */
	private boolean checkTitle(String title) {
		return (!title.equals(""));
	}
	
	/**
	 * validate and add an item to the database
	 * @param title
	 * @param location
	 * @param type
	 * @param dueDate
	 * @param recurType
	 * @param description
	 * @return a ToDoItemManagementMessage 
	 */
	public ToDoItemManagementMessage tryToCreateToDoItem(String title, Location location, int type, Date dueDate, int recurType, String description) {
		ToDoItemManagementMessage returnMessage = new ToDoItemManagementMessage();
		if(!checkTitle(title)) {
			returnMessage.setText(ToDoItemManagementMessage.EMPTY_TITLE_MESSAGE);
		}
		else {
			long newItemId = createToDoItem(title, location, type, dueDate, recurType, description);
			if(newItemId == -1) {
				returnMessage.setText(ToDoItemManagementMessage.CREATE_ITEM_FAILED_MESSAGE);
			}
			else {
				returnMessage.setText(ToDoItemManagementMessage.ITEM_CREATED_MESSAGE);
			}
			
		}
		return returnMessage;
	}
	
	/**
	 * message sent by the ToDoItemManager after performing activities on an item
	 * @author BaoTam
	 *
	 */
	public class ToDoItemManagementMessage {
		public static final String CREATE_ITEM_FAILED_MESSAGE = "Unknown Error. Failed to create item!";
		public static final String EMPTY_TITLE_MESSAGE		= "Title cannot be empty!";
    	public static final String ITEM_CREATED_MESSAGE		= "To Do Item successfully created!";
    	private String message;
    	
    	/**
    	 * create a message with an empty string
    	 */
    	private ToDoItemManagementMessage() {
    		message = "";
    	}
    	
    	/**
    	 * create a message with a given string
    	 * @param message
    	 */
    	private ToDoItemManagementMessage(String message) {
    		this.message = message;
    	}
    	
    	/**
    	 * set the text representation of the message
    	 * @param message
    	 * @return this
    	 */
    	private ToDoItemManagementMessage setText(String message) {
    		this.message = message;
    		return this;
    	}
    	
    	/**
    	 * @return the text representation of the message
    	 */
    	public String getText() {
    		return message;
    	}
    	
    	/**
    	 * @return the text representation of the message
    	 */
    	public String toString() {
    		return getText();
    	}
    	
	}
}
